package com.twq.useage

import scala.math.Ordering

/**
 * Created by tangweiqun on 2017/11/18.
 */
object CollectionSorted {
  def main(args: Array[String]): Unit = {
    //sorted需要一个类型为Ordering[B]的隐式参数
    val seq = Seq(5, 2, 4, 6)
    println(seq.sorted) //默认按照升序排列

    //我们来定义一个隐式转换，使的排序是降序排列
    implicit val intOrdering = new Ordering[Int] {
      def compare(x: Int, y: Int) =
        if (x < y) 1
        else if (x == y) 0
        else -1
    }
    println(seq.sorted)

    //我们可以用sortWith达到上面的两个功能
    //sortWith的底层实现还是sorted
    println(seq.sortWith(_ < _)) //升序排列
    println(seq.sortWith(_ > _)) //降序排列
  }
}
